﻿using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace isky.Plugin
{
    public class UpdatePreYGCBPlugin : IPlugin
    {
        /// <summary>
        /// 报价利润修改触发事件
        /// </summary>
        /// <param name="serviceProvider"></param>
        public void Execute(IServiceProvider serviceProvider)
        {
            CommonWcf commonWcf = new CommonWcf();
            // Obtain the execution context service from the service provider.
            var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            // Obtain the Organization Service factory service from the service provider
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            var service = factory.CreateOrganizationService(context.UserId);
            try
            {
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity inputEntity = context.InputParameters["Target"] as Entity;
                    Entity PreImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains("PreImage")) ? context.PreEntityImages["PreImage"] : null;
                    if (inputEntity.Id == Guid.Empty)
                    {
                        return;
                    }
                    if (inputEntity.Contains("new_cjs"))
                    {
                        //bool calculate=  inputEntity.GetAttributeDefaultValue<bool>("new_calculate",true);
                        //  if (calculate==true)
                        //  {
                        //国内运费(拖车费)
                        decimal domesticfreightq = 0;
                        Money domesticfreightqmongy = commonWcf.GetAttributeInputOrImage<Money>(inputEntity, PreImageEntity, "new_domesticfreightq", null);
                        if (domesticfreightqmongy != null)
                        {
                            domesticfreightq = Convert.ToDecimal(domesticfreightqmongy.Value);
                        }
                        //打托费
                        decimal nursery = 0;
                        Money nurserymoney = commonWcf.GetAttributeInputOrImage<Money>(inputEntity, PreImageEntity, "new_nursery", null);
                        if (nurserymoney != null)
                        {
                            nursery = Convert.ToDecimal(nurserymoney.Value);
                        }
                        //包干费
                        decimal packingcharge = 0;
                        Money packingchargemoney = commonWcf.GetAttributeInputOrImage<Money>(inputEntity, PreImageEntity, "new_packingcharge", null);
                        if (packingchargemoney != null)
                        {
                            packingcharge = Convert.ToDecimal(packingchargemoney.Value);
                        }
                        //业务推广费
                        decimal businesspromotionfee = 0;
                        Money businesspromotionfeemoney = commonWcf.GetAttributeInputOrImage<Money>(inputEntity, PreImageEntity, "new_businesspromotionfee", null);
                        if (businesspromotionfeemoney != null)
                        {
                            businesspromotionfee = Convert.ToDecimal(businesspromotionfeemoney.Value);
                        }
                        //差旅费
                        decimal travelexpenses = 0;
                        Money travelexpensesmoney = commonWcf.GetAttributeInputOrImage<Money>(inputEntity, PreImageEntity, "new_travelexpenses", null);
                        if (travelexpensesmoney != null)
                        {
                            travelexpenses = Convert.ToDecimal(travelexpensesmoney.Value);
                        }
                        //佣金
                        decimal commission = 0;
                        Money commissionmoney = commonWcf.GetAttributeInputOrImage<Money>(inputEntity, PreImageEntity, "new_commission", null);
                        if (commissionmoney != null)
                        {
                            commission = Convert.ToDecimal(commissionmoney.Value);
                        }
                        //其他费用
                        decimal otherexpenses = 0;
                        Money otherexpensesmoney = commonWcf.GetAttributeInputOrImage<Money>(inputEntity, PreImageEntity, "new_otherexpenses", null);
                        if (otherexpensesmoney != null)
                        {
                            otherexpenses = Convert.ToDecimal(otherexpensesmoney.Value);
                        }
                        //利润
                        decimal profites = 0;
                        Money profitesmoney = commonWcf.GetAttributeInputOrImage<Money>(inputEntity, PreImageEntity, "new_profites", null);
                        if (profitesmoney != null)
                        {
                            profites = Convert.ToDecimal(profitesmoney.Value);
                        }
                        //货款资金利息
                        decimal interestonloanfunds = 0;
                        Money interestonloanfundsmoney = commonWcf.GetAttributeInputOrImage<Money>(inputEntity, PreImageEntity, "new_interestonloanfunds", null);
                        if (interestonloanfundsmoney != null)
                        {
                            interestonloanfunds = Convert.ToDecimal(interestonloanfundsmoney.Value);
                        }
                        decimal cjs = (domesticfreightq * Convert.ToDecimal(0.13) / Convert.ToDecimal(1.06) + 
                                      (nursery + packingcharge + businesspromotionfee + travelexpenses + commission + otherexpenses)
                                      * Convert.ToDecimal(0.13) + profites 
                                      * Convert.ToDecimal(0.13) + interestonloanfunds 
                                      * Convert.ToDecimal(0.13))
                                      * Convert.ToDecimal(1.12);

                        if (cjs != 0)
                        {

                            inputEntity.Attributes["new_cjs"] = new Money(Convert.ToDecimal(cjs));
                        }
                        else
                        {
                            inputEntity.Attributes["new_cjs"] = new Money(0);
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
                throw new NotImplementedException();
            }
        }
    }
}
